package com.example.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 阿里短信验证服务工具类
 * @author ..
 * @Date 2023/10/7 9:46
 */
@Component
public class NotificationUtil {

    private static final String signName = "职途万里";// 签名
    private static final String templateCode = "SMS_463652536";// 短信模板


    @Autowired
    private Aliyuncode aliyuncode;

    public boolean sendSms(String phone) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                aliyuncode.getACCESSKEYID(),
                aliyuncode.getSECRET());

        IAcsClient client = new DefaultAcsClient(profile);


        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setPhoneNumbers(phone);
        request.setTemplateParam("{\"name\":\""+phone+"\"}");

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            return true;
        } catch (Exception e) {
            return false;
//            e.printStackTrace();
        }
//        catch (ClientException e) {
//            System.out.println("ErrCode:" + e.getErrCode());
//            System.out.println("ErrMsg:" + e.getErrMsg());
//            System.out.println("RequestId:" + e.getRequestId());
//        }

    }
}
